package cn.pw.test.com.news.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.SchoolNewsAdapter;
import cn.pw.test.com.news.bean.ArticleBean;
import cn.pw.test.com.news.server.NewsBack;
import cn.pw.test.com.news.server.NewsGlobal;
import tech.com.commoncore.base.BaseFragment;

public class SchoolNewsFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";

    private int mParam1;

    private ImageView bannerImg;
    private SmartRefreshLayout smartRefresh;
    private RecyclerView recyclerView;

    private SchoolNewsAdapter schoolNewsAdapter;

    private List<ArticleBean> articleBeans;

    private int page = 1;

    public static SchoolNewsFragment newInstance(int param1) {
        SchoolNewsFragment fragment = new SchoolNewsFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_school_news;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        bannerImg = mContentView.findViewById(R.id.school_news_banner_img);
        smartRefresh = mContentView.findViewById(R.id.smart_refresh_layout);
        recyclerView = mContentView.findViewById(R.id.content_recycler_view);

        schoolNewsAdapter = new SchoolNewsAdapter(R.layout.news_layout_item_school_news);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(schoolNewsAdapter);

        if (mParam1 == 1) {
            bannerImg.setImageResource(R.mipmap.news_icon_school_banner1);
        } else if (mParam1 == 2) {
            bannerImg.setImageResource(R.mipmap.news_icon_school_banner2);
        } else if (mParam1 == 3) {
            bannerImg.setImageResource(R.mipmap.news_icon_school_banner3);
        }

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestData();
            }
        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                requestData();
            }
        });

        if (articleBeans == null || articleBeans.isEmpty()) {
            smartRefresh.autoRefresh();
        }
    }

    private void requestData() {
        String type = "215";
        if (mParam1 == 1) {
            type = "215";
        } else if (mParam1 == 2) {
            type = "214";
        } else if (mParam1 == 3) {
            type = "216";
        }
        NewsGlobal.getNewsImp().requestArticle(page, 10, "id", type, new NewsBack<List<ArticleBean>>() {
            @Override
            public void onBack(List<ArticleBean> beans, String err) {
                smartRefresh.finishRefresh();
                smartRefresh.finishLoadMore();
                if (err == null && beans != null) {
                    articleBeans = beans;
                    schoolNewsAdapter.addData(articleBeans);
                    schoolNewsAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
